#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

// 定义栈的结构
struct Stack {
    int capacity;
    int top;
    int* array;
};

// 创建一个新的栈
struct Stack* createStack(int capacity) {
    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    stack->capacity = capacity;
    stack->top = -1;
    stack->array = (int*)malloc(stack->capacity * sizeof(int));
    return stack;
}

// 检查栈是否已满
int isFull(struct Stack* stack) {
    return stack->top == stack->capacity - 1;
}

// 检查栈是否为空
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// 将元素推入栈
void push(struct Stack* stack, int item) {
    if (isFull(stack)) {
        printf("栈已满，无法推入元素\n");
        return;
    }
    stack->array[++stack->top] = item;
    printf("%d 推入栈\n", item);
}

// 从栈中弹出元素
int pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("栈为空，无法弹出元素\n");
        return -1;
    }
    return stack->array[stack->top--];
}

// 获取栈顶元素
int peek(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("栈为空\n");
        return -1;
    }
    return stack->array[stack->top];
}

int main() {
    struct Stack* stack = createStack(5);

    push(stack, 1);
    push(stack, 2);
    push(stack, 3);
    push(stack, 4);

    printf("栈顶元素: %d\n", peek(stack));

    printf("弹出元素: %d\n", pop(stack));
    printf("弹出元素: %d\n", pop(stack));

    printf("栈顶元素: %d\n", peek(stack));

    return 0;
}